
package com.tools.cleanmaster.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

import com.tools.cleanmaster.CleanConstant;
import com.tools.cleanmaster.common.CommonIntentService;
import com.tools.cleanmaster.manager.TrashManager;
import com.tools.cleanmaster.model.db.TrashesPubApi;
import com.tools.cleanmaster.utils.AppTaskUtils;
import com.tools.cleanmaster.utils.FeatureConfig;
import com.tools.cleanmaster.utils.LogHelper;

import java.net.URI;

public class PackageChangeReceiver extends BroadcastReceiver {
    private static final String TAG = "PackageChangeReceiver";

    private static final boolean DEBUG = FeatureConfig.DEBUG_LOG;

    @Override
    public void onReceive(Context context, Intent intent) {
        boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
        if (DEBUG) {
            LogHelper.i(TAG, "Received: " + intent.getAction() + ", data: " + intent.getDataString() + ", replacing: " + replacing);
        }
        notifyPackageChange(context, intent);

        Intent serviceIntent = new Intent(context, CommonIntentService.class);
        serviceIntent.setAction(CleanConstant.ACTION_PACKAGE_CHANGE);
        serviceIntent.putExtra(CleanConstant.EXTRA_INTENT, intent);

        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                context.startForegroundService(serviceIntent);
            } else {
                context.startService(serviceIntent);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void notifyPackageChange(Context cxt, Intent intent) {
        TrashManager.getInstance().checkNotifyScan();
    }

    /**
     * @param cxt
     * @param intent Not the real Intent from system
     */
    public static void onPackageChange(Context cxt, Intent intent) {
        Intent pkgIntent = intent.getParcelableExtra(CleanConstant.EXTRA_INTENT);
        String action = pkgIntent.getAction();
        boolean replacing = pkgIntent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
        int uid = pkgIntent.getIntExtra(Intent.EXTRA_UID, -1);
        String pkgName = URI.create(pkgIntent.getDataString()).getSchemeSpecificPart();

        if (Intent.ACTION_PACKAGE_ADDED.equals(action) && !replacing) {
            // New app installed
            onPackageAdded(cxt, pkgName, uid);
        } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action) && !replacing) {
            // App removed
            onPackageRemoved(cxt, pkgName, uid);
        } else if (Intent.ACTION_PACKAGE_REPLACED.equals(action)) {
            // App replaced
            onPackageReplaced(cxt, pkgName, uid);
        }

    }

    private static void onPackageRemoved(Context cxt, String pkgName, int uid) {
        // Clear SD files for the removed package
        //TrashesPubApi.onPackageRemoved(cxt, pkgName);
    }

    /**
     * Called when existing app is updated
     */
    private static void onPackageReplaced(Context cxt, String pkgName, int uid) {
        // AD and non-official detect
        // AppMgrPubApi.onPackageAdded(cxt, pkgName);
    }

    /**
     * Called only when new app is installed
     */
    private static void onPackageAdded(Context cxt, String pkgName, int uid) {
        // clear home package set
        AppTaskUtils.clearHomePackages();

        // AD and non-official detect
        //AppMgrPubApi.onPackageAdded(cxt, pkgName);

        // Remove the possible ignore package when installed
        TrashesPubApi.removeIgnorePackage(pkgName);

        // synchronize to protected list if it was showing
        AppTaskUtils.add(pkgName);

        //AdUnlock
        //AutoCleanManager.onPackageAdded(cxt, pkgName);

        //NewResPageUtils.onPackageAdded(cxt, pkgName);
    }
}
